package com.ruoyi.article.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.comment.mapper.CommentMapper;
import com.ruoyi.comment.service.ICommentService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.PageUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.article.domain.Article;
import com.ruoyi.article.service.IArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/article/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;


    @GetMapping("/all")
    public TableDataInfo listAll(){
        startPage();
        Article article = new Article();
        article.setUserId(1L);
        return getDataTable(articleService.selectArticleList(article,null,null));
    }
    /**
     * 查询文章管理列表
     */
    //@PreAuthorize("@ss.hasPermi('article:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(Article article, String nickName, String categoryName)
    {
        startPage();
        List<Article> articles = articleService.selectArticleList(article, nickName, categoryName);
        return getDataTable(articles);
//        if (articleService.selectArticleList(article, nickName, categoryName) == null){
//            System.out.println("==========zoudfaokle");
//            return null;
//        }
//        List<Article> list = articleService.selectArticleList(article, nickName, categoryName);
//                TableDataInfo dataTable = getDataTable(list);
//
//        dataTable.setTotal(articleService.getTotal());
//
//        return dataTable;
    }

    @GetMapping("/categoryId")
    public TableDataInfo articleByCategoryId(Integer categoryId){
        if (articleService.selectArticleByCategoryId(categoryId) == null){
            return null;
        }
        List<Article> articles = articleService.selectArticleByCategoryId(categoryId);
        List<Article> list = new ArrayList<>();
        for (Article article : articles) {
            if (article.getCategoryId() == categoryId){
                list.add(article);
            }
        }
        return getDataTable(list);
    }

    @Autowired
    private ICommentService commentService;
    // 查询文章详情
    @GetMapping("/articleDetails")
    public AjaxResult articleDetails(Integer articleId){
        Article article = articleService.selectArticleDetails(articleId);
        if (article == null){
            return null;
        }
        return AjaxResult.success(article);
//        Article article = articleService.selectArticleDetails(articleId);
//        Integer commentCount = commentService.getCommentCount(articleId);
//        Map<String, Object> map = new HashMap<>();
//        map.put("article",article);
//        map.put("commentCount",commentCount);
//        return AjaxResult.success(map);
    }
    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('article:article:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Article article)
    {
        List<Article> list = articleService.selectArticleList(article,null, null);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        util.exportExcel(response, list, "文章管理数据");
    }
// 查询热搜文章
    @GetMapping("/hotArticle")
    public AjaxResult hotArticle(){
        return AjaxResult.success(articleService.selectHotArticleList());
    }

// 根据文章标题模糊查询
  @GetMapping("/selectArticleByTitle")
  public AjaxResult selectByTitle(String title){
      return AjaxResult.success(articleService.selectArticleListByTitle(title));
  }


    /**
     * 获取文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('article:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(articleService.selectArticleById(id));
    }

    /**
     * 新增文章管理
     */
    @PreAuthorize("@ss.hasPermi('article:article:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Article article)
    {
        return toAjax(articleService.insertArticle(article));
    }

    /**
     * 修改文章管理
     */
    @PreAuthorize("@ss.hasPermi('article:article:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Article article)
    {
        return toAjax(articleService.updateArticle(article));
    }

    /**
     * 删除文章管理
     */
    @PreAuthorize("@ss.hasPermi('article:article:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(articleService.deleteArticleByIds(ids));
    }

    @PutMapping("/save")
    public AjaxResult save(@RequestBody Article article){
        Integer i = articleService.saveNum(article);
        return AjaxResult.success(i);
    }

}
